package cn.edu.cug.cs.gtl.apis.ui;


import cn.edu.cug.cs.gtl.api.wrapper.ProjectWrapper;
import cn.edu.cug.cs.gtl.apis.render.Render;
import cn.edu.cug.cs.gtl.apis.render.nodes.Node;
import cn.edu.cug.cs.gtl.apis.render.nodes.Separator;
import cn.edu.cug.cs.gtl.apis.render.viewers.Viewer;
import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.protos.Project;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class RenderPanel extends JPanel {
    AbstractApplication theApp = null;
    Render render=null;


    public RenderPanel(AbstractApplication theApp,LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        this.render = theApp.getFactory().createRender();
        this.theApp=theApp;
    }

    public RenderPanel(AbstractApplication theApp,LayoutManager layout) {
        super(layout);
        this.render = theApp.getFactory().createRender();
        this.theApp=theApp;
    }

    public RenderPanel(AbstractApplication theApp,boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        this.render = theApp.getFactory().createRender();
        this.theApp=theApp;
    }

    public RenderPanel(AbstractApplication theApp) {
        setLayout(new BorderLayout());
        this.render = theApp.getFactory().createRender();
        this.theApp=theApp;
    }

    public Viewer getViewer() {
        return this.render.getViewer();
    }

    public Node getSceneGraph(){
        return this.render.getSceneGraph();
    }

    public Render getRender() {
        return render;
    }

    public void setRender(Render render) {
        this.render = render;
    }

    public void initial(String fileName){

        Node sceneGraph =null;

        if(fileName!=null) {
            // open chair.iv
            //sceneGraph = theApp.readSceneGraph("$OIVJHOME/data/models/chair.iv");

            try {
                sceneGraph = render.readSceneGraph("D:" + File.separator + "temp" + File.separator + "test.h3d");
                theApp.setProject(render.getProject());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Project p = theApp.getProject();
        if(p==null) {
            theApp.setProject(ProjectWrapper.createDefaultProject());
        }

        Separator root = theApp.getFactory().createSeparator();
        {
            root.addChild(theApp.getFactory().createGradientBackground());
            if(sceneGraph!=null) root.addChild(sceneGraph);
        }

        this.render.setSceneGraph(root);
        getViewer().viewAll();

        add(getViewer().getComponent(), BorderLayout.CENTER);
    }

    public void reset(Node sceneGraph){
        Separator root = theApp.getFactory().createSeparator();
        {
            root.addChild(theApp.getFactory().createGradientBackground());
            root.addChild(sceneGraph);
        }

        this.render.setSceneGraph(root);

        getViewer().viewAll();
    }


}
